package zzs.generalframework.console.core.user.userdetailinner.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import zzs.generalframework.common.core.user.userdetailinner.UserDetailInnerInfo;
import zzs.generalframework.common.core.user.userdetailinner.UserDetailInnerPO;
import zzs.generalframework.console.core.user.userdetailinner.dao.IUserDetailInnerDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.user.userdetailinner.dao.impl
 * @author zzs
 * @date 2018年2月5日
 * @version 1.0
 */
public class UserDetailInnerDaoImpl implements IUserDetailInnerDao
{
	private Logger logger = LoggerFactory.getLogger(UserDetailInnerDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(UserDetailInnerPO userDetailInnerPO)
	{
		logger.info("====插入用户详细(内部)数据开始====");
		logger.debug("====插入用户详细(内部)数据,入参:UserDetailInnerPO=[{}]====", userDetailInnerPO);
		entityManager.persist(userDetailInnerPO);
		logger.debug("====插入用户详细(内部)数据,出参:boolean=[{}]====", true);
		logger.info("====插入用户详细(内部)数据结束====");
		return true;
	}

	@Override
	public boolean update(UserDetailInnerPO userDetailInnerPO)
	{
		logger.info("====更新用户详细(内部)数据开始====");
		logger.debug("====更新用户详细(内部)数据,入参:UserDetailInnerPO=[{}]====", userDetailInnerPO);
		entityManager.merge(userDetailInnerPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新用户详细(内部)数据结束====");
		return true;
	}

	@Override
	public List<UserDetailInnerPO> select(UserDetailInnerInfo userDetailInnerInfo)
	{
		logger.info("====查询用户详细(内部)数据开始====");
		logger.debug("====查询用户详细(内部)数据,入参:UserDetailInnerInfo=[{}]====", userDetailInnerInfo);
		List<UserDetailInnerPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from UserDetailInnerPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(userDetailInnerInfo.getUserDetailInnerID()))
		{
			sqlBuffer.append(" and a.userDetailInnerID=?");
			parameter.putParameterObject(userDetailInnerInfo.getUserDetailInnerID());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询用户详细(内部)数据,出参:List<UserDetailInnerPO>=[{}]====", returnList);
		logger.info("====查询用户详细(内部)数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(UserDetailInnerPO userDetailInnerPO)
	{
		logger.info("====删除用户详细(内部)数据开始====");
		logger.debug("====删除用户详细(内部)数据,入参:UserDetailInnerPO=[{}]====", userDetailInnerPO);
		entityManager.remove(userDetailInnerPO);
		logger.debug("====删除用户详细(内部)数据,出参:boolean=[{}]====", true);
		logger.info("====删除用户详细(内部)数据结束====");
		return true;
	}

	@Override
	public Long count(UserDetailInnerPO userDetailInnerPO)
	{
		logger.info("====统计用户详细(内部)数据开始====");
		logger.debug("====统计用户详细(内部)数据,入参:UserDetailInnerPO=[{}]====", userDetailInnerPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from UserDetailInnerPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(userDetailInnerPO.getUserDetailInnerID()))
		{
			sqlBuffer.append(" and a.userDetailInnerID=?");
			parameter.putParameterObject(userDetailInnerPO.getUserDetailInnerID());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计用户详细(内部)数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计用户详细(内部)数据结束====");
		return returnLong;
	}

}
